#ifndef __LIBRC_H__
#define __LIBRC_H__

#include <stdint.h>

// common

int snprintfcat(char *dst, size_t size, const char *fmt, ...);
int start_timer(int msec);
int check_timer(int tfd);
int close_timer(int tfd);

// nvram

int nvram_get_int(const char *key);
int nvram_set_int(const char *key, int value);
char *nvram_prefix_get (const char *prefix, const char *name);

// atcommand

int atcommand(int fd, const char *cmd, char *res, int ressz, int filter);
int port_open(char *portname);

// modem_cmd

enum {
	MODEM_PIN_READY,
	MODEM_SIM_PIN,
	MODEM_SIM_PUK,
	MODEM_SIM_PIN2,
	MODEM_SIM_PUK2,
};

typedef struct modem_signal {
	int rssi;
	int rscp;
	int ecio;
	int rsrp;
	int rsrq;
	int sinr;
} modem_signal_t;

#define RAT_GSM		0
#define RAT_UTRAN	2
#define RAT_EUTRAN	7

typedef struct modem_netinfo {
	int stat;
	char *stat_s;
	char *oper;
	int rat;
	char *operrat;
	char *mcc;
	char *mnc;
	unsigned int lac;
	unsigned int ci;
} modem_netinfo_t;

typedef struct modem_traf {
	unsigned int last_ds_time;
	uint64_t last_tx_flow;
	uint64_t last_rx_flow;
	unsigned int total_ds_time;
	uint64_t total_tx_flow;
	uint64_t total_rx_flow;
} modem_traf_t;

typedef struct modem_sysinfo {
	int srv_status;
	int srv_domain;
	int roam_status;
	int sim_state;
	int sysmode;
	char *sysmode_name;
	int submode;
	char *submode_name;
} modem_sysinfo_t;

modem_signal_t *modem_get_signal(int fd);
modem_netinfo_t *modem_get_netinfo(int fd);
char *modem_get_model(int fd);
modem_traf_t *modem_get_traffic_info(int fd);
modem_sysinfo_t *modem_get_sysinfo(int fd);

// usb_common

/// USB interface list element
struct ifs_usb_elem {
	int number;	///< interface number (used for data and ui ports)
	int endpoints;	///< number of endpoints (used for data port definition if no config exist)
	int cls;	///< interface class (used if device class == 0)
	int sub;	///< interface subclass
	int prot;	///< protocol type
	char *driver; ///< driver
	struct ifs_usb_elem *next;
};
typedef struct ifs_usb_elem ifs_usb_elem_t;

#define USB_MAX_LOCATION_LEN	32

/// USB device list element
struct dev_usb {
	int bus;	///< bus number
	int parent;	///< parent device (hub) number
	int port;	///< connection port on the parent hub
	int number;	///< this device number
	int cls;	///< device class
	int sub;	///< device subclass
	int prot;	///< protocol type
	int vid;	///< vendor id
	int pid;	///< product id
	char *manuf;	///< manufacturer name
	char *prod;	///< product name
	int num_ifs;	///< number of interfaces in device
	ifs_usb_elem_t *ifs;	///< list of interfaces
	int data_port;	///< main data communication port number
	int ui_port;	///< user interface port for modem controlling, status sms, ussd and so on
	char type;	///< modem type - 'C' for CDMA, 'W' for GPRS/UMTS/LTE, 'T' (unused) for TD-SCDMA
	char loc[USB_MAX_LOCATION_LEN];	///< device location string as 1.2.4
	struct dev_usb *next;
	struct dev_usb *prev;
};
typedef struct dev_usb dev_usb_t;

#define TYPE_NONE		'N'
#define TYPE_CDMA		'C'
#define TYPE_WCDMA		'W'
#define TYPE_TDSCDMA		'T'

dev_usb_t *usb_get_list();
void usb_free_list(dev_usb_t *list);
//dev_usb_t *usb_copy_dev(dev_usb_t *dev);
void usb_free_dev(dev_usb_t *dev);
//dev_usb_t *usb_get_dev_by_loc(const char *loc);

// usbmodem

#define MODEM_PORT_DATA	0
#define MODEM_PORT_UI	1

char *usb_modem_get_port(const char *prefix, int port_type);

#endif /* __LIBRC_H__ */
